import platform from "@/wlkutils/platform";
import courtConfig from "@/base";
import store from "@/wlkutils/store";
// 获取链接参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

// h5登录
export const h5Login = (callback) => {
    if (isWechat()) {
        // 公众号登录
        let host = platform.host();
        let curPage = getCurrentPages();
        let params = curPage[curPage.length - 1].options??{}; //获取当前页面参数，如果有则返回参数的对象，没有参数返回空对象{}
        let payloadObject = {
            host: host,
            event: 'login',
            agent_id:uni.$u.test.isEmpty(uni.getStorageSync('agent_id'))?0:null,
            local: uni.$u.page()+uni.$u.queryParams(params),
        };
        let payload = encodeURIComponent(JSON.stringify(payloadObject));
        uni.$u.http.post('wechat/codeUrl',
            {
                url: courtConfig.apiUrl + '/wechat/oaLogin',
                payload: payload,
            }).then((res) => {
            location.href = res.data
            callback && callback(res)
        })
    } else {
        // 通用登录页--验证码登录
        uni.showModal({
            title: "温馨提示",
            content: "您还未登录，请先登录~",
            confirmText: "去登录",
            cancelText: "再逛会",
            confirmColor:store.state.themeColor,
            success: (res) => {
                if (res.confirm) {
                    store.commit("setLoginPopupShow", true);
                }
            }
        });
    }
}
// 浏览器判断是否微信
export const isWechat = () => {
    let ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        //console.log('是微信客户端')
        return true;
    } else {
        //console.log('不是微信客户端')
        return false;
    }
};